#!/bin/sh

if [ "$1" == "static" ]; then
	STATIC=1
fi

make distclean
qmake -spec macx-g++ -config release fractacular.pro
make

if [ "$STATIC" == "1" ]; then
	mkdir build/Fractacular.app/Contents/Frameworks
	cp -R /Developer/SDKs/MacOSX10.5.sdk/Library/Frameworks/QtCore.framework build/Fractacular.app/Contents/Frameworks/
	cp -R /Developer/SDKs/MacOSX10.5.sdk/Library/Frameworks/QtGui.framework build/Fractacular.app/Contents/Frameworks/

	# We run install_name_tool to set the identification names for the frameworks. The first argument after -id is the new name, and the second argument is the framework which identification we wish to change. The text @executable_path is a special dyld variable telling dyld to start looking where the executable is located. The new names specifies that these frameworks will be located "one directory up and over" in the Frameworks directory.
	install_name_tool -id @executable_path/../Frameworks/QtCore.framework/Versions/4.0/QtCore build/Fractacular.app/Contents/Frameworks/QtCore.framework/Versions/4.0/QtCore
	install_name_tool -id @executable_path/../Frameworks/QtCore.framework/Versions/4.0/QtGui build/Fractacular.app/Contents/Frameworks/QtGui.framework/Versions/4.0/QtGui

	# Now, the dynamic linker knows where to look for QtCore and QtGui. Then we must make the application aware of the library locations as well using install_name_tool's -change mode. This basically comes down to string replacement, to match the identification names that we set for the frameworks.
	install_name_tool -change /Developer/SDKs/MacOSX10.5.sdk/Library/Frameworks/QtCore.framework/Versions/4.0/QtCore  @executable_path/../Frameworks/QtCore.framework/Versions/4.0/QtCore build/Fractacular.app/Contents/MacOS/Fractacular
	install_name_tool -change /Developer/SDKs/MacOSX10.5.sdk/Library/Frameworks/QtGui.framework/Versions/4.0/QtGui  @executable_path/../Frameworks/QtGui.framework/Versions/4.0/QtGui build/Fractacular.app/Contents/MacOS/Fractacular

	# Finally, since the QtGui framework depends on QtCore, we must remember to change the reference for QtGui:
	install_name_tool -change /Developer/SDKs/MacOSX10.5.sdk/Library/Frameworks/QtCore.framework/Versions/4.0/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/4.0/QtCore build/Fractacular.app/Contents/Frameworks/QtGui.framework/Versions/4.0/QtGui
fi
otool -L build/Fractacular.app/Contents/MacOS/Fractacular

# make archive
if [ "$STATIC" == "1" ]; then
	scripts/make-macosx-dmg.sh build Fractacular-with-qt
else
	scripts/make-macosx-dmg.sh build Fractacular
fi
